package com.example.androidxdemo.fragment

import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.widget.addTextChangedListener
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.room.Room
import com.example.androidxdemo.App
import com.example.androidxdemo.R
import com.example.androidxdemo.adapter.RoomFunctionAdapter
import com.example.androidxdemo.database.AppDatabase
import com.example.androidxdemo.databinding.FragmentFunctionDatabaseBinding
import com.example.androidxdemo.entity.Function

/**
 * @author : yaobing
 * @date   : 2020/7/20 9:53
 * @desc   :
 */

class FunctionDatabaseFragment : BaseFragment() {
    var binding: FragmentFunctionDatabaseBinding? = null


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val functionNameCheck = false
        val functionDesCheck = false
        binding = FragmentFunctionDatabaseBinding.inflate(inflater, container, false)
        binding?.rlFragmentTitle?.fragmentTitleDes?.text = getString(R.string.RoomExample)
        initView(functionNameCheck, functionDesCheck)
        return binding?.root
    }

    private fun initView(functionNameCheck: Boolean, functionDesCheck: Boolean) {
        var functionNameCheck1 = functionNameCheck
        var functionDesCheck1 = functionDesCheck
        var functions: List<Function>
        lateinit var db:AppDatabase
        val adapter = RoomFunctionAdapter()

        Thread(Runnable {
            binding?.rvFunction?.layoutManager = LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false)
            db = AppDatabase.getInstance(requireContext().applicationContext)
            adapter.setDB(db)
            binding?.rvFunction?.adapter = adapter
        }).start()

        binding?.etFunctionName?.addTextChangedListener {
            if (!TextUtils.isEmpty(it.toString())) {
                functionNameCheck1 = true
                setAddEnable(functionDesCheck1, functionNameCheck1)
            } else {
                functionNameCheck1 = false
                setAddEnable(functionDesCheck1, functionNameCheck1)
            }
        }

        binding?.etFunctionDes?.addTextChangedListener {
            if (!TextUtils.isEmpty(it.toString())) {
                functionDesCheck1 = true
                setAddEnable(functionDesCheck1, functionNameCheck1)
            } else {
                functionDesCheck1 = false
                setAddEnable(functionDesCheck1, functionNameCheck1)
            }
        }

        binding?.btFuncionAdd?.setOnClickListener {
            val funcion = Function()
            funcion.functionName = binding?.etFunctionName?.text.toString()
            funcion.functionDetail = binding?.etFunctionDes?.text.toString()
            Thread(Runnable {
                db.plantDao().addFunction(funcion)
                updateList(adapter, db.plantDao().getFunctionList())
            }).start()
        }

        binding?.btFuncionQuery?.setOnClickListener {
            Thread(Runnable {
                db = AppDatabase.getInstance(requireContext().applicationContext)
                functions = db.plantDao().getFunctionList()
                updateList(adapter, functions)
            }).start()
        }

        Thread(Runnable {
            db = AppDatabase.getInstance(requireContext().applicationContext)
            functions = db.plantDao().getFunctionList()
            updateList(adapter, functions)
        }).start()
    }

    private fun updateList(adapter: RoomFunctionAdapter, functions: List<Function>) {
        activity?.runOnUiThread {
            adapter.setList(functions)
        }
    }

    private fun setAddEnable(functionDesCheck: Boolean, functionNameCheck: Boolean) {
        binding?.btFuncionAdd?.isEnabled = functionDesCheck && functionNameCheck
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

    }

    override fun onDestroyView() {
        super.onDestroyView()
        binding = null
    }
}